package com.zhang.admin.service.impl;

import com.zhang.admin.dao.SysElementMapper;
import com.zhang.admin.dao.SysRoleMapper;
import com.zhang.admin.dao.SysUserMapper;
import com.zhang.admin.dao.SysUserRoleMapper;
import com.zhang.admin.po.SysUserRole;
import com.zhang.admin.service.UserService;
import com.zhang.admin.vo.*;
import com.zhang.common.core.constant.StatusConstant;
import com.zhang.common.core.dto.UserInfo;
import com.zhang.common.core.handle.ExceptionMsg;
import com.zhang.common.core.page.Navigator;
import com.zhang.common.core.page.Pagination;
import com.zhang.common.core.po.admin.SysUser;
import com.zhang.common.core.util.CommonUtils;
import com.zhang.common.core.util.EntityUtil;
import com.zhang.common.security.util.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
@Transactional(rollbackFor = {Exception.class})
public class UserServiceImpl implements UserService {


    private final SysUserMapper sysUserMapper;
    private final SysRoleMapper sysRoleMapper;
    private final SysElementMapper sysElementMapper;
    private final SysUserRoleMapper sysUserRoleMapper;

    @Override
    public UserInfo findByUsername(String username) {

        ExceptionMsg.checkedDataNull(username, "username", ExceptionMsg.check_param);

        SysUser sysUser = sysUserMapper.findByUsername(username, StatusConstant.DELETE_FLAG_N);
        ExceptionMsg.checkedDataNull(sysUser, "sysUser", ExceptionMsg.check_result);

        Integer userId = sysUser.getUserId();
        List<String> permissions = sysElementMapper.queryPermissionByUserId(userId,StatusConstant.DELETE_FLAG_N);

        List<String> roleCode = sysRoleMapper.queryRoleCodeByUserId(userId,StatusConstant.DELETE_FLAG_N);

        return new UserInfo(sysUser, permissions, roleCode);


    }

    @Override
    public void createUser(CreateUpdateUserParams createUserVo) {
        ExceptionMsg.checkedDataNull(createUserVo, "createUserVo", ExceptionMsg.check_param);
        //TODO 验重 username
        SysUser sysUser = new SysUser();
        EntityUtil.setCreatAndUpdatInfo(sysUser);
        BeanUtils.copyProperties(createUserVo, sysUser);
        sysUser.setSalt(CommonUtils.getUUID());
        sysUser.setAvatar("头像地址");
        sysUserMapper.insertSelective(sysUser);

        List<Integer> roleIds = createUserVo.getRoleIds();

        List<SysUserRole> sysUserRoleList  = new ArrayList<>();
        if(roleIds != null && roleIds.size()>0){
            roleIds.forEach(roleId -> sysUserRoleList.add(new SysUserRole(sysUser.getUserId(),roleId)));
            sysUserRoleMapper.insertBatch(sysUserRoleList);
        }

    }



    @Override
    public void deleteUser(DeleteUserParams deleteUserVo) {
        sysUserMapper.updateDelFlagByUserId(deleteUserVo.getUserId(),StatusConstant.DELETE_FLAG_Y);
        sysUserRoleMapper.deleteByUserId(deleteUserVo.getUserId());
    }

    @Override
    public void updateUser(UpdateUserParams updateUserVo) {

    }

    @Override
    public Navigator<SysUser> querySysUserByPage(QueryUserByPageParams querySysUserByPageParams) {

        List list = sysUserMapper.querySysUserByPage(querySysUserByPageParams);
        List<SysUser> sysUserList = (List<SysUser>)(list.get(0));
        List<Integer> countList = (List<Integer>) (list.get(1));
        Integer totalCount = countList.get(0);
        Pagination pagination = new Pagination(querySysUserByPageParams.getPageSize(),querySysUserByPageParams.getPageNum(),CommonUtils.camelToUnderline(querySysUserByPageParams.getField()),querySysUserByPageParams.getSort().toLowerCase());
        Navigator<SysUser> page = new Navigator<>(pagination,totalCount,sysUserList);

        return page;
    }

    @Override
    public ViewUserVo currentUserInfo() {
        String username = SecurityUtils.getUser().getUsername();
        SysUser sysUser = sysUserMapper.findByUsername(username, StatusConstant.DELETE_FLAG_N);
        ViewUserVo viewUserVo = new ViewUserVo();
        BeanUtils.copyProperties(sysUser,viewUserVo);
        Integer userId = sysUser.getUserId();
        List<String> permissions = sysElementMapper.queryPermissionByUserId(userId,StatusConstant.DELETE_FLAG_N);
        List<String> roleCode = sysRoleMapper.queryRoleCodeByUserId(userId,StatusConstant.DELETE_FLAG_N);
        return viewUserVo;

    }


}
